package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.WaterBill;
import com.ruoyi.system.mapper.WaterBillMapper;
import com.ruoyi.system.service.IWaterBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 水费账单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-23
 */
@Service
public class WaterBillServiceImpl implements IWaterBillService
{
    @Autowired
    private WaterBillMapper waterBillMapper;

    /**
     * 查询水费账单
     *
     * @param id 水费账单主键
     * @return 水费账单
     */
    @Override
    public WaterBill selectWaterBillById(Long id)
    {
        return waterBillMapper.selectWaterBillById(id);
    }

    @Override
    public String getBillNumber() {
        // 从配置获取城市代码
       /* String cityCode = configService.getConfigValue("system.city.code");
        if (cityCode == null) {
            cityCode = "bill"; // 默认郑州
        }*/

        String datePart = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMM"));
        List<WaterBill> waterBills = waterBillMapper.selectWaterBillList(null);
        if(waterBills.isEmpty()){
            return datePart + "000001";
        }else{
            WaterBill lastBill = waterBills.get(0);
            String lastBillNumber = lastBill.getBillNumber();
            String lastBillDate = lastBillNumber.substring(0, 6);
            if(lastBillDate.equals(datePart)){
                int lastSequence = Integer.parseInt(lastBillNumber.substring(6));
                return datePart + String.format("%06d", lastSequence + 1);
            }else{
                return datePart + "000001";
            }
        }
    }


    /**
     * 查询水费账单列表
     *
     * @param waterBill 水费账单
     * @return 水费账单
     */
    @Override
    public List<WaterBill> selectWaterBillList(WaterBill waterBill)
    {
        return waterBillMapper.selectWaterBillList(waterBill);
    }

    @Override
    public List<WaterBill> selectOverdueBillList(WaterBill waterBill) {
        return waterBillMapper.selectOverdueBillList(waterBill);
    }

    /**
     * 新增水费账单
     *
     * @param waterBill 水费账单
     * @return 结果
     */
    @Override
    public int insertWaterBill(WaterBill waterBill)
    {
        waterBill.setCreateTime(DateUtils.getNowDate());
        String month = DateUtils.parseDateToStr("yyyy-MM", waterBill.getAdviceDate());
        waterBill.setMonth(month);
        return waterBillMapper.insertWaterBill(waterBill);
    }

    /**
     * 修改水费账单
     *
     * @param waterBill 水费账单
     * @return 结果
     */
    @Override
    public int updateWaterBill(WaterBill waterBill)
    {
        waterBill.setUpdateTime(DateUtils.getNowDate());
        return waterBillMapper.updateWaterBill(waterBill);
    }

    /**
     * 批量删除水费账单
     *
     * @param ids 需要删除的水费账单主键
     * @return 结果
     */
    @Override
    public int deleteWaterBillByIds(Long[] ids)
    {
        return waterBillMapper.deleteWaterBillByIds(ids);
    }

    /**
     * 删除水费账单信息
     *
     * @param id 水费账单主键
     * @return 结果
     */
    @Override
    public int deleteWaterBillById(Long id)
    {
        return waterBillMapper.deleteWaterBillById(id);
    }
}
